package cn.yqx.bankshop.oauth2.controller;


import cn.yqx.bankshop.oauth2.config.SCaptcha;
import cn.yqx.bankshop.oauth2.store.VcodeManager;
import com.alibaba.druid.util.StringUtils;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Controller
public class LoginController {

    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";
    public static final int EXPIRE_MINUTE_TIME = 5 ;
    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private  VcodeManager vcodeManager ;
    @RequestMapping("/code/image")
    public void imageCode(HttpServletRequest request, HttpServletResponse response,String authImgKey) throws IOException {

        log.info("authImgKey {} " , authImgKey );
        SCaptcha scaptcha = new SCaptcha() ;

        vcodeManager.saveImageCode(authImgKey,scaptcha.getCode(),EXPIRE_MINUTE_TIME, TimeUnit.MINUTES);

        scaptcha.write(response.getOutputStream());

    }
}
